<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Form for filling out a shift report.
 *
 * @category    RedCross
 * @package     Core_Form
 */
class Core_Form_Shift_Report extends RedCross_Form
{
	const REPLACEMENT_PREFIX = 'replacement';
	const REPLACEMENT_EXPLODE_KEY = '_';
	const VALUE_NO_SHOW = -1;
	const ACTION = 'submitreport';
	
	public function init()
	{
        $this->setAttrib('id','report_form');
		$action = $this->createElement('hidden', 'action');
		$action->setValue(self::ACTION);
		$this->addElement($action);
				
    	$this->addElement('hidden', 'shift_id');
		
    	$this->addElement('textarea','injuries',array(
    		'rows'		=> 10,
    		'cols'		=> 60,
    		'required'	=> true,
            'label'		=> 'Skader',
			'filters'		=> array('StringTrim'),
//            'validators'    =>  array(
//                array('NotEmpty', true),
//                array('StringLength', true, array(0,255)),
//            )
		));
				
    	$this->addElement('textarea','equipment_list',array(
    		'rows'		=> 5,
    		'cols'		=> 60,    	
    		'required'	=> true,
            'label'		=> 'Forbrukt materiell',
			'filters'		=> array('StringTrim'),
//            'validators'    =>  array(
//                array('NotEmpty', true),
//                array('StringLength', true, array(0,255)),
//            )    		
		));

		$this->addElement('textarea','report',array(
    		'rows'		=> 3,
    		'cols'		=> 60,		
            'required'	=> true,
            'label'		=> 'Meldinger',
			'filters'		=> array('StringTrim'),
//            'validators'    =>  array(
//                array('NotEmpty', true),
//                array('StringLength', true, array(0,255)),
//            )			
		));		
		
	}
	
	public function fillForm($shift)
	{
		$memberService = RedCross_Factory::getService('Core_Service_Member');
		$members = $memberService->fetchAllOrderByLastnameInactiveLast();
		$this->getElement('shift_id')->setValue($shift->id);

		$shiftHasSubmittedShiftReport = !is_null($shift->shift_report_id);
		if ($shiftHasSubmittedShiftReport)
		{
			$this->getElement('report')->setValue($shift->ShiftReport->report);
			$this->getElement('equipment_list')->setValue($shift->ShiftReport->equipment_list);
			$this->getElement('injuries')->setValue($shift->ShiftReport->injuries);
		}
		
		foreach ($shift->Roles as $shiftRole)
		{
			$roleName = $shiftRole->Role->name;
			$originalMember = $shiftRole->Member;
			/* FIXME: someome familiar with this need to probe the lagic abit */
			if (is_null($originalMember))
				continue;

			$originalMemberName = $originalMember->first_name . ' ' . $originalMember->last_name;

			$attendedMember = $shiftRole->Attendant;

			if ($shiftHasSubmittedShiftReport)
			{
				$selectedValue = is_null($attendedMember) ? self::VALUE_NO_SHOW : $attendedMember->id;
			}
			else
			{
				$selectedValue = $originalMember->id;
			}
            
            $name = self::REPLACEMENT_PREFIX . self::REPLACEMENT_EXPLODE_KEY . $shiftRole->id;
            $this->addElement('select',$name,array(
                'required' => true

            ));

            $dropdown = $this->getElement($name);
            $dropdown->setLabel($shiftRole->Role->name . ': ' . $originalMemberName);
            $dropdown->addDecorators(array('Label','Description','Errors'));
            $label = $dropdown->getDecorator('label');
            $label->setoption('tag','<li>');
            
            $dropdown->addMultiOption($originalMember->id,'Møtte opp');
			$dropdown->addMultiOption(self::VALUE_NO_SHOW,'Møtte ikke opp, ikke erstattet');

            foreach ($members as $member)
			{
				if ((int)$member->id != (int)$shiftRole->Member->id)
				{
					$dropdown->addMultiOption($member->id, 'Erstattet av ' . $member->getFullName());
				}
			}

            $dropdown->setValue($selectedValue);
            /*
			$dropdown = $this->createElement('select',self::REPLACEMENT_PREFIX . self::REPLACEMENT_EXPLODE_KEY . $shiftRole->id);
			$dropdown->setLabel($shiftRole->Role->name . ': ' . $originalMemberName);

			$dropdown->addMultiOption($originalMember->id,'Møtte opp');
			$dropdown->addMultiOption(self::VALUE_NO_SHOW,'Møtte ikke opp, ikke erstattet');
			foreach ($members as $member)
			{
				if ((int)$member->id != (int)$shiftRole->Member->id)
				{
					$dropdown->addMultiOption($member->id, 'Erstattet av ' . $member->first_name . ' ' . $member->last_name);
				}
			}			
			$dropdown->setValue($selectedValue);
			$this->addElement($dropdown);
			*/
		}
		
		$this->addSubmitButton();
	}
	
	private function addSubmitButton()
	{
		$this->addElement('submit', 'submit', array(
            'required' => true,
            'ignore'   => true,
            'label'    => 'Send inn',
        ));		
	}
}

